package arithmetic;

/**
 * 使用链表来实现先进先出队列，核心思想，链表的头部取元素，链表的尾部插入元素
 */
public class LinkedQueue<Item> {
    private int N;

    private Node firstNode; //头部

    private Node nextNode; //尾部

    private class Node {
        private Item item;
        private Node next;
    }

    //添加元素
    public void enqueue(Item item) {
        Node node = new Node();
        node.item = item;
        if (firstNode == null && nextNode == null) {
            this.firstNode = this.nextNode = node;
        }
        nextNode.next = node;
        N++;

    }

    //取出元素
    public Item dequeue() {
        if (firstNode == null) {
            return null;
        }
        Item item = firstNode.item;
        firstNode = firstNode.next;
        N--;
        return item;
    }
}
